package cn.doitedu.mr;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.*;
import java.net.URI;
import java.util.HashMap;

/**
 * @date: 2019/7/11
 * @site: www.doitedu.cn
 * @author: hunter.d 涛哥
 * @qq: 657270652
 * @description:  电影评分记录转hbase csv文件
 *
 * 需求：将3类文件拼接
 * 方案：map端join
 *
 */
public class Movie2CSV {

    public static class M extends Mapper<LongWritable, Text,Text, NullWritable>{
        HashMap<String, String> movieInfo = new HashMap<>();
        HashMap<String, String> userInfo = new HashMap<>();
        Text k = new Text();
        NullWritable v = NullWritable.get();

        @Override
        protected void setup(Context context) throws IOException, InterruptedException {


            loadDict("movies.dat",movieInfo);
            loadDict("users.dat",userInfo);

        }


        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] split = line.split("\"");
            String movieId = split[3];
            String rate = split[7];
            String timestamp = split[11];
            String uid = split[15];

            String mInfo = movieInfo.get(movieId);
            String uInfo = userInfo.get(uid);


            // 填充movied为统一长度(在hbase rowkey设计中非常重要)
            String padMid = StringUtils.leftPad(movieId, 6, "0");
            String padUid = StringUtils.leftPad(uid, 6, "0");

            k.set(padMid+timestamp+padUid + "@" +mInfo + "@" + uInfo + "@" + rate);
            context.write(k,v);
        }




        private void loadDict(String path, HashMap<String, String> info) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
            String line = null;

            // 加载字典
            while((line=br.readLine())!=null){
                // 74::Bed of Roses (1996)::Drama|Romance
                String[] split = line.split("::");
                if(split.length>2){
                    info.put(split[0],split[1]+"@"+split[2]);
                }
            }
            br.close();
        }

    }


    // hadoop jar wc.jar -->  java -cp wc.jar:$HADOOPE_HOME/share/lib/*.jar:$HADOOP_HOME/etc/hadoop/hdfs-site.xml:$
    public static void main(String[] args) throws Exception {


        Configuration conf = new Configuration();  // 自动加载classpath中的core-*.xml  hdfs-*.xml mapred-*.xml yarn-*.xml

        Job job = Job.getInstance(conf);

        job.setJarByClass(Movie2CSV.class);

        job.setMapperClass(M.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(NullWritable.class);

        job.setNumReduceTasks(0);

        job.addCacheFile(new URI("hdfs://spark01:8020/cach/movies.dat"));
        job.addCacheFile(new URI("hdfs://spark01:8020/cach/users.dat"));

        FileInputFormat.setInputPaths(job,new Path("hdfs://spark01:8020/movie/input"));
        FileOutputFormat.setOutputPath(job,new Path("hdfs://spark01:8020/movie/csv"));

        boolean b = job.waitForCompletion(true);
        System.exit(b?0:1);

    }


}
